//
//  TopSecondViewController.m
//  Products_B
//
//  Created by lanou on 16/6/3.
//  Copyright © 2016年 朋学友. All rights reserved.
//

#import "TopSecondViewController.h"
#import "TopSecondModel.h"
#import "TopSecondCollectionViewCell.h"
#import "TopSecondCollectionReusableView.h"
#import "ShopDetailViewController.h"

#define kTopSecondURL @"http://api.izhangchu.com/"
@interface TopSecondViewController () <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSMutableArray *modelArray;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) TopSecondCollectionReusableView *header;
@property (nonatomic, strong) NSString *contentString;
@property (nonatomic, strong) NSString *introString;

@end

@implementation TopSecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 64) collectionViewLayout:flowLayout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    // 注册cell
    [_collectionView registerClass:[TopSecondCollectionViewCell class] forCellWithReuseIdentifier:@"topSecondCollectionViewCell"];
    // 注册表头
    [_collectionView registerClass:[TopSecondCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    [self requestData];
}


- (void)requestData {
    NSDictionary *parDic = @{@"methodName":@"MallSpecial",@"version":@4.3,@"id":@(_id),@"_time":@1464918816,@"_signature":@"478847b3a8adaf016fbb35fc8d76c4ab"};
    [RequestManager requestWithUrl:kTopSecondURL requestType:RequestTypePOST parDic:parDic finish:^(NSData *data) {
        
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSLog(@" ************ %@",jsonDic);
        self.modelArray = [TopSecondModel topSecondModelAnalyseDataWithJsonDic:jsonDic];
        
        _introString = jsonDic[@"data"][@"specialIntro"];
        _contentString = jsonDic[@"data"][@"specialDesc"];
        self.navigationItem.title = jsonDic[@"data"][@"specialTitle"];
        // 设置导航栏字体大小
        [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16] forKey:NSFontAttributeName]];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [_collectionView reloadData];
        });
    } error:^(NSError *error) {
        NSLog(@"请求数据失败");
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _modelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TopSecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"topSecondCollectionViewCell" forIndexPath:indexPath];
    cell.topModel = self.modelArray[indexPath.row];
    return cell;
}

// 返回cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(kWidth / 2 - 7.5, kWidth / 2 - 7.5 + 120);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        _header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        _header.introLabel.text = _introString;
        _header.contentLabel.text = _contentString;
        return _header;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(kWidth, _header.introLabel.frame.size.height + _header.contentLabel.frame.size.height + 10);
}

#pragma mark - 点击cell的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ShopDetailViewController *shopDetailVC = [[ShopDetailViewController alloc] init];
    TopSecondModel *topModel = self.modelArray[indexPath.row];
    shopDetailVC.id = topModel.id;
    [self.navigationController pushViewController:shopDetailVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
